package com.fastCms.manager.frame.controller;

import com.fastCms.business.frame.busi.CloudUserManageService;
import com.fastCms.business.frame.exception.BusinessException;
import com.fastCms.business.frame.pojo.PageParam;
import com.fastCms.business.frame.pojo.UserModel;
import com.fastCms.business.frame.pojo.entity.CloudUser;
import com.fastCms.business.frame.pojo.po.*;
import com.fastCms.business.frame.pojo.vo.CloudUserVO;
import com.fastCms.business.frame.response.ApiResponse;
import com.fastCms.manager.frame.annotation.UserAccess;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 哈密瓜科技 版权所有 © Copyright 2019<br>
 *
 * @Description: 登录权限控制<br>
 * @Project: hami <br>
 * @CreateDate: Created in 2019/3/7 下午3:48 <br>
 * @Author: <a href="lsquan666@163.com">lsquan</a>
 */
@RestController
@RequestMapping("/cloudUser")
@Api(value = "CloudUserController", tags = "后端-用户帐号管理")
public class CloudUserManageController {
    @Autowired
    private CloudUserManageService cloudUserManageServiceImpl;

    @UserAccess
    @ApiOperation(value = "登录", notes = "账号+密码登录")
    @PostMapping(value = "/login")
    public ApiResponse<UserModel> login(@RequestBody CloudLoginUserPO loginUserPO, HttpServletRequest request) {
        ApiResponse apiResponse = cloudUserManageServiceImpl.loginAuth(loginUserPO, request);
        return apiResponse;
    }


    @ApiOperation(value = "用户列表", notes = "平台下的用户列表")
    @PostMapping(value = "/showPage")
    public ApiResponse<PageInfo<CloudUserVO>> showPage(@RequestBody @Valid PageParam<CloudUserPO> cloudUserPO) {
        ApiResponse<PageInfo<CloudUserVO>> result = cloudUserManageServiceImpl.showPage(cloudUserPO);
        return result;
    }

    @ApiOperation(value = "新建用户", notes = "角色下的用户")
    @PostMapping(value = "/addCloudUser")
    public ApiResponse<CloudUser> addCloudUser(@RequestBody @Valid AddCloudUserPO cloudUserPO) throws BusinessException {
        ApiResponse<CloudUser> apiResponse = cloudUserManageServiceImpl.addCloudUser(cloudUserPO);
        return apiResponse;
    }

    @ApiOperation(value = "修改用户信息", notes = "用户名无法修改")
    @PostMapping(value = "/updateCloudUser")
    public ApiResponse<CloudUser> updateCloudUser(@RequestBody @Valid UpdateCloudUserPO updateCloudUserPO) throws BusinessException {
        ApiResponse<CloudUser> apiResponse = cloudUserManageServiceImpl.updateCloudUser(updateCloudUserPO);
        return apiResponse;
    }

    @ApiOperation(value = "给用户分配角色", notes = "给用户配置角色")
    @PostMapping(value = "/useRelRole")
    public ApiResponse<CloudUser> useRelRole(@RequestBody @Valid CloudUserRelRolePO param) {
        ApiResponse<CloudUser> apiResponse = cloudUserManageServiceImpl.useRelRole(param);
        return apiResponse;
    }

    @ApiOperation(value = "删除用户", notes = "删除用户&角色关联关系")
    @PostMapping(value = "/deleteCloudUser/{userId}")
    public ApiResponse deleteCloudUser(@PathVariable String userId) {
        ApiResponse apiResponse = cloudUserManageServiceImpl.deleteCloudUser(userId);
        return apiResponse;
    }

    @ApiOperation(value = "禁用和激活用户", notes = "删除用户&角色关联关系")
    @PostMapping(value = "/changeStatus")
    public ApiResponse<Boolean> changeUserStatus(@RequestBody CloudUserEditStatusPO param) {
        ApiResponse apiResponse = cloudUserManageServiceImpl.changeStatus(param);
        return apiResponse;
    }

    @ApiOperation(value = "用户密码修改")
    @PostMapping(value = "/resetPassword")
    public ApiResponse<Boolean> resetPassword(@RequestBody @Valid UserResetPwdPO resetPwdPO) {
        ApiResponse apiResponse = cloudUserManageServiceImpl.resetPwd(resetPwdPO);
        return apiResponse;
    }
}
